﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Data;
using System.Data.SqlServerCe;
using System.Windows.Data;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;

namespace BambolettaDB
{
    public class MainVM: BaseVM
    {
        #region Properties and Fields
        private ObservableCollection<RequestVM> _Requests;
        public ObservableCollection<RequestVM> Requests
        {
            get { return _Requests; }
            set { _Requests = value; NotifyPropertyChanged("Requests"); }
        }

        private ListCollectionView _RequestsView;
        public ListCollectionView RequestsView
        {
            get { return _RequestsView; }
            set { _RequestsView = value; NotifyPropertyChanged("RequestsView"); }
        }

        private ObservableCollection<FilterBaseVM> _Filters = new ObservableCollection<FilterBaseVM>();
        public ObservableCollection<FilterBaseVM> Filters
        {
            get { return _Filters; }
            set { _Filters = value; NotifyPropertyChanged("Filters"); }
        }

        #endregion

        #region Commands
        private DelegateCommand _ManageDollsCommand;
        public ICommand ManageDollsCommand
        {
            get { return _ManageDollsCommand; }
        }
        public void ManageDollsExecuted(object parameter)
        {
            ManageDollsDialog dlg = new ManageDollsDialog();
            dlg.Owner = Application.Current.MainWindow;
            dlg.ShowDialog();
            UpdateRequestView();
        }

        private DelegateCommand _ManageCustomersCommand;
        public ICommand ManageCustomersCommand
        {
            get { return _ManageCustomersCommand; }
        }
        public void ManageCustomersExecuted(object parameter)
        {
            ManageCustomersDialog dlg = new ManageCustomersDialog();
            dlg.Owner = Application.Current.MainWindow;
            dlg.ShowDialog();
            Requests = DBCommands.GetRequests();
            UpdateRequestView();
        }

        private DelegateCommand _AboutCommand;
        public ICommand AboutCommand
        {
            get { return _AboutCommand; }
        }
        public void AboutExecuted(object parameter)
        {
            AboutDialog dlg = new AboutDialog();
            dlg.Owner = Application.Current.MainWindow;
            dlg.ShowDialog();
        }

        private DelegateCommand _AddRequestCommand;
        public ICommand AddRequestCommand
        {
            get { return _AddRequestCommand; }
        }
        public void AddRequestExecuted(object parameter)
        {
            AddRequestDialog dlg = new AddRequestDialog();
            dlg.Owner = Application.Current.MainWindow;
            dlg.ShowDialog();
            UpdateRequestView();
        }

        private DelegateCommand _AddFilterCommand;
        public ICommand AddFilterCommand
        {
            get { return _AddFilterCommand; }
        }
        public void AddFilterExecuted(object parameter)
        {
            AddFilterDialog dlg = new AddFilterDialog();
            dlg.Owner = Application.Current.MainWindow;
            dlg.ShowDialog();
        }
        #endregion
        
        #region Constructor
        public MainVM()
        {
            _ManageDollsCommand = new DelegateCommand(new Action<object>(ManageDollsExecuted));
            _ManageCustomersCommand = new DelegateCommand(new Action<object>(ManageCustomersExecuted));
            _AboutCommand = new DelegateCommand(new Action<object>(AboutExecuted));
            _AddRequestCommand = new DelegateCommand(new Action<object>(AddRequestExecuted));
            _AddFilterCommand = new DelegateCommand(new Action<object>(AddFilterExecuted));

            if (!App.IsInDesignMode)
            {
                PasswordDialog dlg = new PasswordDialog();
                dlg.ShowDialog();
                if (!DBCommands.TestConnection())
                {
                    MessageBox.Show("Invalid Password.");
                    Application.Current.MainWindow.Close();
                }

                UpdateRequestView();
            }

            Filters.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Filters_CollectionChanged);
        }

        #endregion

        #region Public methods
        void Filters_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            RequestsView.Refresh();
        }

        public bool EvaluateFilter(object obj)
        {
            RequestVM request = obj as RequestVM;
            if (Filters.Count > 0 && request != null)
            {
                foreach (FilterBaseVM filter in Filters)
                {
                    if (!filter.Evaluate(request))
                        return false;
                }
            }

            return true;
        }
        #endregion

        #region Utility Methods
        void UpdateRequestView()
        {
            Requests = DBCommands.GetRequests();
            RequestsView = new ListCollectionView(Requests);
            RequestsView.Filter = new Predicate<object>(EvaluateFilter);
        }
        #endregion

        #region Singleton accessor
        public static MainVM GetInstance()
        {
            MainWindow main = Application.Current.MainWindow as MainWindow;
            if (main != null)
            {
                MainVM vm = main.Resources["Main"] as MainVM;
                if (vm != null)
                    return vm;
            }
            return null;
        }
        #endregion

    }
}
